package com.tangkun.environment.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tangkun.environment.R;
import com.tangkun.environment.entity.xinwen.TypeReturn;

import java.util.ArrayList;
import java.util.List;

/**
 * @author tangkun
 * @ClassName: ModifyInfectionAdapter
 * @Description: 咨询分类适配器
 * @date 2016-08-20 上午11:57:36
 */
public class ZiXunFeiLeiAdapter extends BaseAdapter {

    private Context context;
    private List<TypeReturn.DataBean> listData = new ArrayList<TypeReturn.DataBean>();

    public ZiXunFeiLeiAdapter(Context context, List<TypeReturn.DataBean> listData) {
        this.context = context;
        this.listData = listData;
    }

    static class ViewHolder {
        /**
         * 父控件
         */
        public LinearLayout bgLL;
        /**
         * 名称
         */
        public TextView nameTv;
        /**
         * 是否选中
         */
        public ImageView selectedIv;
    }

    @Override
    public int getCount() {
        return listData.size();
    }

    @Override
    public Object getItem(int i) {
        return listData.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_zixunfeilei, null);
            holder = new ViewHolder();
            holder.bgLL = (LinearLayout) convertView.findViewById(R.id.ll_zixunfeilei);
            holder.selectedIv = (ImageView) convertView.findViewById(R.id.iv_zixunfeilei_select);
            holder.nameTv = (TextView) convertView.findViewById(R.id.tv_zixunfeilei_name);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (listData != null) {
            // 名称
            if (!TextUtils.isEmpty(listData.get(position).val)) {
                holder.nameTv.setText(listData.get(position).val);
            } else {
                holder.nameTv.setText("");
            }
            //是否被选中
            if (listData.get(position).isSelected) {
                holder.selectedIv.setVisibility(View.VISIBLE);
                holder.selectedIv.setImageResource(R.mipmap.ic_gouxuan);
            } else {
                holder.selectedIv.setVisibility(View.GONE);
                holder.selectedIv.setImageResource(0);
            }
        }
        return convertView;

    }

    /**
     * 自定义接口回调
     */
    private MyClickListener myClickListener;

    public void setMyClickListener(MyClickListener myClickListener) {
        this.myClickListener = myClickListener;
    }

    public interface MyClickListener {
        void onMyClick(int position);
    }

}
